package bubuntux.jusbscan.core;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import jcurses.system.Toolkit;

import org.apache.commons.lang.StringUtils;

import bubuntux.util.CommandLine;
import bubuntux.util.Unix;

public abstract class Configs {

	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
			.getBundle("jUSB-Scan");

	public static final String antivirus = initAntivirus();
	public static final int terminalCount = initTerminalCount();
	public static final boolean bell = getBoolean("Bell");
	public static final String exclude = getString("Exclude"); // Can be null
	public static final String maxFileSize = initMaxFileSize();
	public static final int maxDirRecursion = initMaxDirRecursion();

	public static final boolean showInfected = getBoolean("ShowInfected");
	public static final boolean showEmptyFiles = getBoolean("ShowEmptyFiles");
	public static final boolean showOK = getBoolean("ShowOK");
	public static final boolean showExcluded = getBoolean("ShowExcluded");
	public static final boolean showOthers = getBoolean("ShowOthers");

	// TODO remplace this variables, improvement jcurses
	public static final int stringLength = initStringLength();
	public static final int listHeigth = initListHeigth();

	public static void beep() {
		if (bell) {
			Toolkit.beep(); // Make a beep
		}
	}

	public static int getFilesCount(String path) {
		return Unix.execUniqueInt(new CommandLine("sh", "-c", "find " + path
				+ " -type f | wc -l"));
	}

	private static boolean getBoolean(String key) {
		String value = getString(key);
		boolean val = Boolean.valueOf(value);
		if (value != null && StringUtils.startsWithIgnoreCase(value, "y")) {
			val = true;
		}

		return val;
	}

	private static int getInt(String key) {
		int val = -1;
		String value = getString(key);
		if (StringUtils.isAlphanumericSpace(value)) {
			val = Integer.parseInt(value);
		}
		return val;
	}

	private static String getString(String key) {
		String value = null;
		try {
			value = RESOURCE_BUNDLE.getString(key);
		} catch (MissingResourceException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		} catch (ClassCastException e) {
			e.printStackTrace();
		}
		return value;
	}

	private static String initAntivirus() {
		String antivirus = getString("Antivirus");
		if (antivirus == null) {
			antivirus = "ClamAv"; // Default
		}

		return antivirus;
	}

	private static int initListHeigth() {
		int width;

		if (terminalCount >= 2 && terminalCount <= 6) {
			width = 10;
		} else if (terminalCount == 1) {
			width = 24;
		} else if (terminalCount >= 7 && terminalCount <= 12) {
			width = 6;
		} else {
			width = 3;
		}

		return width;
	}

	private static int initMaxDirRecursion() {
		int maxDirRecursion = getInt("MaxDirRecursion");
		if (maxDirRecursion <= 0) {
			maxDirRecursion = 15; // Default
		}
		return maxDirRecursion;
	}

	private static String initMaxFileSize() {
		String maxFileSize = getString("MaxFileSize");
		if (maxFileSize == null) {
			maxFileSize = "25MB"; // Default
		}

		return maxFileSize;
	}

	private static int initStringLength() {
		int length;

		if (terminalCount >= 2 && terminalCount <= 4) {
			length = 47;
		} else if (terminalCount == 1) {
			length = 96;
		} else if (terminalCount >= 5 && terminalCount <= 9) {
			length = 31;
		} else if (terminalCount >= 10 && terminalCount <= 12) {
			length = 22;
		} else {
			length = 10;
		}

		return length;
	}

	private static int initTerminalCount() {
		int terminalCount = getInt("TerminalCount");
		if (terminalCount <= 0 || terminalCount > 12) { // 12 is Enough!!!
			terminalCount = 4; // Default
		}
		return terminalCount;
	}
}
